package cn.driver.reserve.mapper;

import cn.driver.common.pojo.Coach;
import cn.driver.common.pojo.Course;
import cn.driver.common.pojo.Reserve;
import cn.driver.common.pojo.Student;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 预约mapper
 *
 * @author Jingkai Wang
 */

@Mapper
public interface ReserveMapper {
    @Select("select resId, coaId, stuId, courseId, resTime, `comment`,`status` from reserve where resId like concat('%',#{resId},'%') limit #{index},#{pageSize}")
    @Results(id = "reserveMap", value = {
            @Result(column = "resId", property = "resId"),
            @Result(column = "coaId", property = "coaId"),
            @Result(column = "stuId", property = "stuId"),
            @Result(column = "courseId", property = "courseId"),
            @Result(column = "resTime", property = "resTime"),
            @Result(column = "comment", property = "comment"),
            @Result(column = "status", property = "status"),
            @Result(property = "coach", column = "coaId", one = @One(select = "cn.driver.reserve.mapper.ReserveMapper.getCoachInReserve", fetchType = FetchType.EAGER)),
            @Result(property = "student", column = "stuId", one = @One(select = "cn.driver.reserve.mapper.ReserveMapper.getStudentInReserve", fetchType = FetchType.EAGER)),
            @Result(property = "course", column = "courseId", one = @One(select = "cn.driver.reserve.mapper.ReserveMapper.getCourseInReserve", fetchType = FetchType.EAGER))
    })
    List<Reserve> getReserveList(int index, int pageSize, String resId);

    @Select("select resId, coaId, stuId, courseId, resTime, `comment`,`status` from reserve where stuId = #{stuId}")
    @ResultMap("reserveMap")
    Reserve getReserveByStuId(int stuId);

    @Select("select count(1) from reserve where resId like concat(#{date},'%')")
    int getCountByCreateDate(String date);

    @Select("select count(1) from reserve")
    int getReserveCount();

    @Select("select resId, coaId, stuId, courseId, resTime, `comment`,`status` from reserve where coaId = #{coaId}")
    @ResultMap("reserveMap")
    List<Reserve> getReserveListByCoach(int coaId);

    @Insert("insert into reserve (resId, coaId, stuId, courseId, resTime, `comment`) values (#{resId},#{coaId},#{stuId},#{courseId},#{resTime},#{comment})")
    int insert(Reserve reserve);

    @Update({"<script>",
            "update reserve",
            "   <set>",
            "       <if test='coaId != null'>coaId = #{coaId},</if>",
            "       <if test='stuId != null'>stuId = #{stuId},</if>",
            "       <if test='courseId != null'>courseId = #{courseId},</if>",
            "       <if test='resTime != null'>resTime = #{resTime},</if>",
            "       <if test='comment != null and comment != \"\"'>`comment` = #{comment}</if>",
            "   </set>",
            "where resId = #{resId}",
            "</script>"})
    int update(Reserve reserve);

    @Update("update reserve set status = 1 where resId = #{resId}")
    int changeStatus(String resId);

    @Delete("delete from reserve where resId = #{resId}")
    int delete(String resId);

    @Select("select account, stuName from student where id = #{id}")
    Student getStudentInReserve(int id);

    @Select("select courseName from course where id = #{id}")
    Course getCourseInReserve(int id);

    @Select("select account, coachName from coach where id = #{id}")
    Coach getCoachInReserve(int id);
}
